import { Component, OnInit } from '@angular/core';
import { RequestService } from '@app/api/request';
import { Router } from '@angular/router';
import md5 from 'js-md5';
import {
  AbstractControl,
  FormBuilder,
  FormGroup,
  Validators
} from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  public validateForm: FormGroup;

  constructor(
    private fb: FormBuilder, 
    private http: RequestService,
    private msg: NzMessageService,
    private router: Router
  ) {}

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      userName: [ null, [ Validators.required ] ],
      password: [ null, [ Validators.required ] ],
      remember: [ true ]
    });
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
      this.validateForm.controls[ i ].updateValueAndValidity();
    }
    if (this.validateForm.controls.userName.value && this.validateForm.controls.password.value) {
      const params = {
        userName: this.validateForm.controls.userName.value,
        password: md5(this.validateForm.controls.password.value)
      }
      console.log(params)
      this.router.navigateByUrl('dashboard');
    }
  }

}
